<?php
class Store extends CI_Controller {
	 function __construct()
	{
		parent::__construct();	
	    $this->load->model('member_model');
		$this->load->model('category_model');
		$this->load->model('favorite_model');
		$this->load->model('product_model');
	}
     
	 function getPath($action = '')
	{
		return array('controller' => strtolower(__CLASS__), 'action' => strtolower($action));
	}
	function index()
	{
		$data['title']		= "Supermarket Home";
		$newCount                 = 8;
		$specialCount             = 2;
		$where['deleted']         = 0;
		$product                  = $this->product_model->getProducts(0, $newCount , $like, $where,'' );
		$view['product'] 		  = $product->records;
		$special                  = $this->product_model->getProducts(0, $specialCount , $like, array('deleted'=>0,'special'=>1),'' );
		$view['special'] 		  = $special->records;
		$categories         		  = $this->category_model->getCategories($page,'', $like,array('deleted' => 0,'cat_type'=>'p'));
		$data['categories'] 		  = $categories->records;
		$this->layout->getView('header',$data);
		$this->layout->getView('index', $view);
		$this->layout->getView('footer');

	}
	
	
	function aboutus()
	{
		$categories         		  = $this->category_model->getCategories($page,'', $like,array('deleted' => 0,'cat_type'=>'p'));
		$data['categories'] 		  = $categories->records;
		$this->layout->getView('header',$data);
		$this->layout->getView('static/aboutus_top', $view);
		$this->layout->getView('static/aboutus', $view);
		$this->layout->getView('static/aboutus_bottom', $view);
		$this->layout->getView('footer');
	}
	function explore()
	{
		$categories         		  = $this->category_model->getCategories($page,'', $like,array('deleted' => 0,'cat_type'=>'p'));
		$data['categories'] 		  = $categories->records;
		$this->layout->getView('header',$data);
		$this->layout->getView('explore', $view);
		$this->layout->getView('footer');
		
	}
	function offer()
	{
		$categories         		  = $this->category_model->getCategories($page,'', $like,array('deleted' => 0,'cat_type'=>'p'));
		$data['categories'] 		  = $categories->records;
		$this->layout->getView('header',$data);
		$this->layout->getView('static/offer_top', $view);
		$this->layout->getView('static/offer', $view);
		$this->layout->getView('static/offer_bottom', $view);
		$this->layout->getView('footer');
		
	}
	function terms()
	{
		//$this->layout->getView('header');
		$this->layout->getView('static/terms_top', $view);
		$this->layout->getView('static/terms', $view);
		$this->layout->getView('static/terms_bottom', $view);
		//$this->layout->getView('footer');
		
	}
	function contact()
	{
		$categories         		  = $this->category_model->getCategories($page,'', $like,array('deleted' => 0,'cat_type'=>'p'));
		$data['categories'] 		  = $categories->records;
		$this->layout->getView('header',$data);
		$this->layout->getView('static/contact_top', $view);
		$this->layout->getView('static/contact', $view);
		$this->layout->getView('static/contact_bottom', $view);
		$this->layout->getView('footer');
	}
	function sitemap()
	{
		$categories         		  = $this->category_model->getCategories($page,'', $like,array('deleted' => 0,'cat_type'=>'p'));
		$data['categories'] 		  = $categories->records;
		$this->layout->getView('header',$data);
		$this->layout->getView('site_map', $view);
		$this->layout->getView('footer');
	}
	function  products($page = 0,$cat='')
	{
		    $per_page_count 	     = $this->config->item('per_page_count');
			$view 				     = $this->getPath(__FUNCTION__);
    		
		    /************** categories ****************************/
			$where                       = array('deleted' => 0);
			if(!empty($cat))
			{
				
				$where['category']        =  $cat;
				$this->session->set_userdata(array('category' => $cat)); 
				if($cat=='all')
				{
					unset($where['category']);
				}
			}
			$s_cat = $this->session->userdata('category');
			if(!empty($s_cat))
			{
				$where['category']        =  $s_cat;
				if($s_cat=='all')
				{
					unset($where['category']);
				}
			}
			
			$categories         		  = $this->category_model->getCategories($page,'', $like,array('deleted' => 0,'cat_type'=>'p'));
			$view['categories'] 		  = $categories->records;
			 /************** categories ****************************/
			 
			  /************** products ****************************/
			$products           		  = $this->product_model->getProducts($page,$per_page_count, $like,$where,'');
			$view['products']    		  = $products->records;
			$view['cur_page'] 	          = $page; //secho $view['cur_page'];
			$view['title']		          = 'Prodct Listing';
			$view['subtitle']	          = 'Product Lists';
			/************************ Pagination block ***********************************/
			$config['base_url'] 	      = $this->config->item('base_url').'store/products/';
			$config['total_rows'] 	      = $products->total; 
			$config['per_page']           = $per_page_count;  //echo  $view['cur_page'];
			$config['cur_page']           = $page /$per_page_count; 
			$config['uri_segment'] 	      = 3;
			$config['num_links'] 	      = 3;
			$this->pagination->initialize($config);
			$this->layout->getView('header',$view);
			$this->layout->getView('home',$view);
			$this->load->view('footer'); 
		
	}
	function favorite($id='')
	{
		$mylogin  = $this->session->userdata('mylogin');
		$data['user_id']           = $mylogin['id'];
		$data['product_id']        = $id;
		$insertStatus              = $this->favorite_model->add($data);
		switch($insertStatus)
			{
				case 1:
					$this->session->set_flashdata('product_msg', "fgggggggggggg");
					if(!empty($cur_page)) 
						redirect($this->config->item('base_url').'store/favorites/'.$cur_page,'location');
					else
						redirect($this->config->item('base_url').'store/favorites/','location');
					exit;		
					break;
				case -1:
					break;
				case -2:
					$view['msg'] 	= $this->lang->line('product_valid_failed');
					break;
				case -3:
					$view['msg'] 	= $this->lang->line('product_already_exists');
					break;
				case false:
					$view['msg'] 	= $this->lang->line('product_add_failure');
	
					break;
				
				default:
					$view['msg'] 	= $this->lang->line('product_add_failure');
					break;
			}
		
	}
	function favorites()
	{
		$mylogin                      = $this->session->userdata('mylogin');
		$favorite                     = $this->favorite_model->getFavorites(0, '' , $like, array('user_id'=>$mylogin['id']) );
		$view['favorite'] 		      = $favorite->records;
		$categories         		  = $this->category_model->getCategories($page,'', $like,array('deleted' => 0,'cat_type'=>'p'));
		$data['categories'] 		  = $categories->records;
		$this->layout->getView('header',$data);
		$this->layout->getView('favorite',$view);
		$this->load->view('footer'); 
		
	}
	
	
}
?>